/*
 * Created on Oct 10, 2004
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package org.vectrics.history.hibernate;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.hibernate.CallbackException;
import org.hibernate.Interceptor;
import org.hibernate.Transaction;
import org.hibernate.classic.Session;
import org.hibernate.type.Type;
import org.vectrics.common.db.Auditable;
import org.vectrics.hibernate.HibernateUtil;


/**
 * @author mmoore
 */
public class AuditLogInterceptor {//implements Interceptor {
	private Session session;
	private Long personId;
	private Set inserts = new HashSet();
	private Set updates = new HashSet();
	private HibernateUtil hibernate = null;


	public AuditLogInterceptor(HibernateUtil hibernate) {
		this.hibernate = hibernate;
	}

	public Boolean isTransient(Object obj) {
		return new Boolean(false);
	}

	public void afterTransactionBegin(Transaction transaction) {

	}

	public void afterTransactionCompletes(Transaction transaction) {

	}

	public void beforeTransactionCompletes(Transaction transaction) {

	}

	public String getEntityName(Object obj) {
		return(obj.toString());
	}

	/* (non-Javadoc)
	 * @see org.hibernate.Interceptor#onSave(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
	 */
	public boolean onSave(Object entity, Serializable arg1, Object[] state,
			String[] propertyNames, Type[] types) throws CallbackException {
		if (entity instanceof Auditable) {
			inserts.add(entity);
		}

		return false;
	}

	/* (non-Javadoc)
	 * @see org.hibernate.Interceptor#onFlushDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
	 */
	public boolean onFlushDirty(Object entity, Serializable id, Object[] currentState,
			Object[] previousState, String[] propertyNames, Type[] types) throws CallbackException {
		if (entity instanceof Auditable) {
			updates.add(entity);
		}
		return false;
	}



	/**
	 * @return Returns the personId.
	 */
	public Long getPersonId() {
		return personId;
	}
	/**
	 * @param personId The personId to set.
	 */
	public void setPersonId(Long personId) {
		this.personId = personId;
	}
	/**
	 * @return Returns the session.
	 */
	public Session getSession() {
		return session;
	}
	/**
	 * @param session The session to set.
	 */
	public void setSession(Session session) {
		this.session = session;
	}
	/* (non-Javadoc)
	 * @see org.hibernate.Interceptor#onLoad(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
	 */
	public boolean onLoad(Object arg0, Serializable arg1, Object[] arg2,
			String[] arg3, Type[] arg4) throws CallbackException {
		// TODO Auto-generated method stub
		return false;
	}


	public void postFlush(Iterator arg0) throws CallbackException {
		try {
			for (Iterator it = inserts.iterator(); it.hasNext();) {
				Auditable entity = (Auditable)it.next();
				AuditLog.logEvent("create", entity, personId, hibernate, session.connection());
			}
			for (Iterator it = updates.iterator(); it.hasNext();) {
				Auditable entity = (Auditable)it.next();
				AuditLog.logEvent("create", entity, personId, hibernate, session.connection());
			}
		} catch (Exception ex) {
			throw new CallbackException(ex);
		} finally {
			inserts.clear();
			updates.clear();
		}
	}

	/* (non-Javadoc)
	 * @see org.hibernate.Interceptor#preFlush(java.util.Iterator)
	 */
	public void preFlush(Iterator arg0) throws CallbackException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.hibernate.Interceptor#onDelete(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
	 */
	public void onDelete(Object arg0, Serializable arg1, Object[] arg2,
			String[] arg3, Type[] arg4) throws CallbackException {
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see org.hibernate.Interceptor#isUnsaved(java.lang.Object)
	 */
	public Boolean isUnsaved(Object arg0) {
		// TODO Auto-generated method stub
		return new Boolean(false);
	}

	/* (non-Javadoc)
	 * @see org.hibernate.Interceptor#findDirty(java.lang.Object, java.io.Serializable, java.lang.Object[], java.lang.Object[], java.lang.String[], org.hibernate.type.Type[])
	 */
	public int[] findDirty(Object arg0, Serializable arg1, Object[] arg2,
			Object[] arg3, String[] arg4, Type[] arg5) {
		// TODO Auto-generated method stub
		return null;
	}

	/* (non-Javadoc)
	 * @see org.hibernate.Interceptor#instantiate(java.lang.Class, java.io.Serializable)
	 */
	public Object instantiate(Class arg0, Serializable arg1)
			throws CallbackException {
		// TODO Auto-generated method stub
		return null;
	}

}
